package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.service.ChannelService;
import com.heima.model.admin.dtos.AdChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, AdChannel> implements ChannelService {

    @Override
    public ResponseResult findByNameAndPage(AdChannelDto adChannelDto) {


        //1.检查参数是否正确
        adChannelDto.checkParam();
        //2.封装查询条件(查询参数\分页参数)

        //分页参数
        IPage<AdChannel> pageRequest = new Page<>(adChannelDto.getPage(), adChannelDto.getSize());

        //查询条件
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.<AdChannel>lambdaQuery();
        //判断非空
        if(StringUtils.isNotBlank(adChannelDto.getName())){
            queryWrapper.like(AdChannel::getName, adChannelDto.getName());
        }

        //3.得到分页结果
        IPage<AdChannel> pageResult = this.page(pageRequest,queryWrapper);

        //4.封装PageResponseResult
        //设置分页
        PageResponseResult pageResponseResult =
                new PageResponseResult(adChannelDto.getPage(), adChannelDto.getSize(), pageResult.getTotal());
        //设置参数
        pageResponseResult.setData(pageResult.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult insert(AdChannel adChannel) {

        ResponseResult responseResult = new ResponseResult();
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.<AdChannel>lambdaQuery();

        //1.检查参数(name不能为重复值,数据库改为unique)
        if(StringUtils.isBlank(adChannel.getName())){
            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"频道名称不能为空");
        }
        //2.查询默认值是否勾选,如果已经勾选,查询表中默认值的字段并更改为不为默认值
        if(adChannel.getIsDefault()&&adChannel.getIsDefault()!=null){
            List<AdChannel> list = list(Wrappers.
                    <AdChannel>lambdaQuery().eq(AdChannel::getIsDefault,true));

            //将数据修改为false
            list.forEach((channel)->{channel.setIsDefault(false);
            updateById(channel);
            });
            /*queryWrapper.eq(AdChannel::getIsDefault,adChannel.getIsDefault());
            AdChannel adChannel1 = this.getOne(queryWrapper);
            adChannel.setIsDefault(false);
            queryWrapper.eq(AdChannel::getId,adChannel1.getId());
            this.update(adChannel1,queryWrapper);*/
        }
        //3.替换成功,更新日期后 插入操作
        adChannel.setCreatedTime(new Date());
        this.save(adChannel);

        //4.返回200
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delete(Integer id) {
        AdChannel adChannel = getById(id);
        if(adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,
                    "要删除的频道不存在"); }
        if(adChannel.getStatus()==true){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,
                    "频道已启用,无法删除"); }
        this.removeById(adChannel.getId());
        return ResponseResult.okResult();
    }
}
